/**
 * Author: zhjpaul
 * Description: 用于与Flask后台进行数据的交互
 */


/**
 * 获取交换机所有物理端口信息
 * 
 * @return {void}
 */

function getPhyPortsStatus(renderSwitchPhyPort, attachEventToPort) {

    $.get(CONFIG.URL.GetPhyPortsStatus, function (data) {

        showPortTraffic(0);

        gPhyPortsStatus = data['phy_ports'];

        for (var i = 0, len = gPhyPortsStatus.length; i < len; i += 2) {
            renderSwitchPhyPort(i, gPhyPortsStatus[i]['phy_tap_flag'], gPhyPortsStatus[i]['phy_port_alias']); 
        }

        for (var i = 1, len = gPhyPortsStatus.length; i < len; i += 2) {
            renderSwitchPhyPort(i, gPhyPortsStatus[i]['phy_tap_flag'], gPhyPortsStatus[i]['phy_port_alias']); 
        }
        
        attachEventToPort();
    })

}

/**
 * 更新交换机某个物理端口信息
 * 
 * @param  {Array} portStatus  交换机端口信息
 * @return {void}
 */

function updatePhyPortStatus(portStatus) {

    $.post(CONFIG.URL.UpdatePhyPortStatus, portStatus, function (data) {
        alert("update port status successfully!!!");
    })

}

/**
 * 获取交换机上所有LAG端口的信息
 * 
 * @return {void}
 */

function getLAGPortsStatus() {

    $.get(CONFIG.URL.GetLAGPortsStatus, function (data) {

        gLAGPortsStatus = data['lag_ports'];

        for (var i = 0, len = gLAGPortsStatus.length; i < len; i++) {
            renderLAG(gLAGPortsStatus[i]);
        }

    });

}

/**
 * 更新某个LAG端口的信息，一旦这个LAG端口上没有任何包含的物理端口时删除此LAG端口
 *  
 * @param  {Object} lagInfo 用于描述一个LAG端口
 * @return {void}
 */

function updateLAGPortStatus(lagInfo) {

    $.post(CONFIG.URL.UpdateLAGPortStatus, lagInfo, function (data) {
        alert("update LAG port successfully!!!");
    });

}

/**
 * 获取端口的流量信息
 * @param  {Object} portInfo 描述获取哪个端口的信息
 * @return {void}
 */

function getPortTrafficStat(portInfo) {

    $.post(CONFIG.URL.GetPortTrafficStat, portInfo, function (data) {

        var traffic = data['outport_traffic_stats'],
            pieData = [];

        // 初始化Traffic表格
        $("#table tbody").html('');

        for (var i = 0, len = traffic.length; i < len; i++) {

            var line = [],
                id = parseInt(traffic[i]['outport_id']),
                trafficInfo = {
                    'outport': gPhyPortsStatus[id]['phy_port_alias'],
                    'traffic': traffic[i]['outport_traffic'],
                    'tapPort': parseInt(traffic[i]['tapped_port_id'])
                };

            renderTrafficTable(trafficInfo);

            line.push(gPhyPortsStatus[id]['phy_port_alias']);
            line.push(parseFloat(traffic[i]['traffic_percent']));

            pieData.push(line);
        }

        drawPie(pieData);

    });

}

/**
 * 获取当前所有流表项信息
 * 
 * @return {void}
 */

function getFlowEntries() {

    $.get(CONFIG.URL.GetFlowEntries, function (data) {

        var flowEntries = data['flow_entries'];

        for (var i = 0, len = flowEntries.length; i < len; i++) {
            gFlowEntries[flowEntries[i]['flow_id']] = flowEntries[i];
            renderFlowEntry(flowEntries[i]);
        }

    });

}

/**
 * 添加流表项
 * 
 * @param {Object} flowEntry 流表项各字段数据
 * @return {void}
 */

function addFlowEntry(flowEntry) {

    $.post(CONFIG.URL.AddFlowEntry, flowEntry, function (data) {
        alert("add flow entry successfully!!!");
        flowEntry['flow_id'] = data['flow_id'];
        renderFlowEntry(flowEntry);
    })

}

/**
 * 修改流表项
 * 
 * @param  {Object} flowEntry 流表项各字段数据
 * @return {void}
 */

function modifyFlowEntry(flowEntry) {

    flowEntry['target_flow_id'] = flowEntry['flow_id'];

    $.post(CONFIG.URL.ModifyFlowEntry, flowEntry, function (data) {
        alert("modify flow entry successfully!!!");
    });

}

/**
 * 删除流表项
 * 
 * @param  {Object} flowEntry 流表项ID
 * @return {void}
 */

function deleteFlowEntry(flowID) {

    $.post(CONFIG.URL.DeleteFlowEntry, flowID, function (data) {
        alert("delete flow entry successfully!!!");
    });

}

/**
 * 配置控制器
 * 
 * @param  {Object} controllerInfo 控制器信息（名字，ip与端口）
 * @return {void}
 */

function configController(controllerInfo) {

    $.post(CONFIG.URL.ConfigController, controllerInfo, function (data) {
        alert("update controller information successfully!!!");
    })

}